################################################################################ 
#
# Counterspeech encouraging users to adopt the perspective of minority 
# groups reduces hate speech and its amplification on social media
#
# SRU model
#
################################################################################ 


rm(list = ls())
setsave = T

################################################################################ 
#  LIBRARIES
################################################################################ 

library(dplyr)
library(tidyr)
library(xtable)
library(foreign)
library(systemfit)
library(stringr)
#library(readr) 

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = ''
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')

data = read.csv(paste0(wd_data, '/dataset_analysis.csv'))
data = data[!(data$user_deleted == "yes"),]

# treatment dummy 
data$treated_dummy <- ifelse(data$group_number == 5, 0, 1)


################################################################################ 
#   VARIABLES
################################################################################

outvars = c('orig_tweet_deleted', # "total_tweets_post",
            'tox_dummy_p_key_num',
            'tox_dummy_p_key')
outvars_name = c( 'Xenophobic Tweet Deleted', #'# Total Tweets',
                  'No. of Xenophobic Tweets',
                  'Xenophobic Tweet Share')

treatments = c('empathy', 'traditional', 'analogical', 'perspective_getting', 'disapproval')
treatments_lab = c( 'Empathy', 'Traditional Perspective Taking',  'Analogical Perspective Taking',
                    'Perspective Getting', 'Disapproval')

################################################################################ 
#  SRU models 
################################################################################ 

# Restrict the sample to observations without missing outcomes
data = data[which(!is.na(data$tox_dummy_p_key) & 
                    !is.na(data$tox_dummy_p_key_num) &
                    !is.na(data$orig_tweet_deleted)),]



#1  Analogical perspective taking 
temp = data[data$group_name %in% c('control', 'analogical'),]
models = list()
models[['hate']] = tox_dummy_p_key_num ~ treated_dummy
models[['hateshare']] = tox_dummy_p_key ~ treated_dummy
models[['deleted']] = orig_tweet_deleted ~ treated_dummy

fitsur1 <- systemfit(models, method = "SUR", data=temp)


#2 Traditional perspective taking 

temp = data[data$group_name %in% c('control', 'traditional'),]

models = list()
models[['hate']] = tox_dummy_p_key_num ~ treated_dummy
models[['hateshare']] = tox_dummy_p_key ~ treated_dummy
models[['deleted']] = orig_tweet_deleted ~ treated_dummy

fitsur2 <- systemfit(models, method = "SUR", data=temp)


#3 perspective getting

temp = data[data$group_name %in% c('control', 'getting'),]

models = list()
models[['hate']] = tox_dummy_p_key_num ~ treated_dummy
models[['hateshare']] = tox_dummy_p_key ~ treated_dummy
models[['deleted']] = orig_tweet_deleted ~ treated_dummy

fitsur3 <- systemfit(models, data=temp, method = "SUR")


#4 empathy

temp = data[data$group_name %in% c('control', 'getting', 'traditional', 'analogical'),]
models = list()
models[['hate']] = tox_dummy_p_key_num ~ treated_dummy
models[['hateshare']] = tox_dummy_p_key ~ treated_dummy
models[['deleted']] = orig_tweet_deleted ~ treated_dummy

fitsur4 <- systemfit(models, data=temp, method = "SUR")


#5 disapproval
temp = data[data$group_name %in% c('control', 'disapproval'),]
models = list()
models[['hate']] = tox_dummy_p_key_num ~ treated_dummy
models[['hateshare']] = tox_dummy_p_key ~ treated_dummy
models[['deleted']] = orig_tweet_deleted ~ treated_dummy
# models[['total']] = total_tweets_post ~ treated_dummy

fitsur5 <- systemfit(models, data=temp, method = "SUR")



# Testing joint insignificance

restriction = paste0(c('hate', 'hateshare', 'deleted'), '_treated_dummy =0')
  
out = data.frame()
out = rbind(out, c('analogical perspective taking', linearHypothesis(fitsur1, restriction, white.adjust = "hc1")$`Pr(>F)`[2]))
out = rbind(out, c('traditional perspective taking', linearHypothesis(fitsur2, restriction, white.adjust = "hc1")$`Pr(>F)`[2]))
out = rbind(out, c('perspective getting', linearHypothesis(fitsur3, restriction, white.adjust = "hc1")$`Pr(>F)`[2]))
out = rbind(out, c('empathy', linearHypothesis(fitsur4, restriction, white.adjust = "hc1")$`Pr(>F)`[2]))
out = rbind(out, c('disapproval', linearHypothesis(fitsur5, restriction, white.adjust = "hc1")$`Pr(>F)`[2]))

colnames(out) = c('treatment', 'pval joint significance')

if (setsave){write.csv(out, paste0(wd_res, '/tables/sru_model_main_outcomes.csv'), row.names = F)}
 
